import {reactive} from "vue";
import {Star} from "./Star";
import {Item, itemData, itemIdNameMap, itemMap, loadItemData} from "./Item";
import {Planet} from "./Planet.ts";
import {loadStarTypeData, starTypeData, starTypeMap} from "../ts/StarType.ts";
import {loadPlanetTypeResourceData, planetTypeResourceData, planetTypeResourceMap} from "../ts/PlanetTypeItem.ts";
import {loadPlanetTypeData, planetTypeData, planetTypeMap} from "./PlanetType.ts";
import {build, normal} from "./Config.ts";
import {PlanetResource} from "./PlanetResource.ts";
import {loadOceanData, oceanData, oceanIdNameMap, oceanMap} from "./Ocean.ts";
import {PlanetMachine} from "./PlanetMachine.ts";

loadItemData()
loadOceanData()
loadStarTypeData()
loadPlanetTypeData()
loadPlanetTypeResourceData()

class Data {
    lastSaveTime = 0
    moveLeftTime = 0
    chooseTab = 'galaxy'

    config = normal

    oceanData = oceanData
    oceanMap = oceanMap
    oceanIdNameMap = oceanIdNameMap

    itemData = itemData
    itemMap = itemMap
    itemIdNameMap = itemIdNameMap

    // resourceData = new Array<Resource>()

    starTypeData = starTypeData
    starTypeMap = starTypeMap

    planetTypeData = planetTypeData
    planetTypeMap = planetTypeMap

    planetTypeResourceData = planetTypeResourceData
    planetTypeResourceMap = planetTypeResourceMap

    filter = new Filter()

    starData = new Array<Star>()
    starMap = new Map<number, Star>()

    planetData = new Array<Planet>()
    planetMap = new Map<number, Planet>()

    planetResourceData = new Array<PlanetResource>()
    planetResourceMap = new Map<number, PlanetResource>()

    planetMachineData = new Array<PlanetMachine>()
    planetMachineMap = new Map<number, PlanetMachine>()

    starVoMap = new Map<number, StarVo>()
    planetVoMap = new Map<number, PlanetVo>()

    current = new StarWithPlanet(new StarVo(), new PlanetVo())
    target = new StarWithPlanet(new StarVo(), new PlanetVo())

    inventory = new Array<Item>()
}


export class Filter {
    starTypeId = 0
    resourceTypeId = 0
    oceanTypeId = 0
    inventoryTypeId = 0
    storeTypeId = 0
    hotPlanetTypeId = 2
    warmPlanetTypeId = 1
    coldPlanetTypeId = 3
    gasPlanetTypeId = 21
    planetType = ''
    onlyCurrentStar = false
    limitShow = 10
}

export class StarVo extends Star {

    planetArray: Array<PlanetVo> = []
}

export class PlanetVo extends Planet {

    itemMap = new Map<number, Item>()

    machineMap = new Map<number, Item>()

}

export class StarWithPlanet {
    star: StarVo
    planet: PlanetVo

    constructor(star: StarVo, planet: PlanetVo) {
        this.star = star;
        this.planet = planet;
    }

}

let cache = localStorage.getItem('save')
let temp;
if (!cache) {
    temp = Object.assign(new Data())
    build()
} else {
    temp = Object.assign(new Data(), JSON.parse(cache))

    for (let star of temp.starData) {
        temp.starMap.set(star.id, star)
        temp.starVoMap.set(star.id, Object.assign(new StarVo(), star))
    }

    for (let planet of temp.planetData) {
        temp.planetMap.set(planet.id, planet)
        temp.planetVoMap.set(planet.id, Object.assign(new PlanetVo(), planet))
        temp.starVoMap.get(planet.starId).planetArray.push(temp.planetVoMap.get(planet.id))
    }

    for (let pr of temp.planetResourceData) {
        temp.planetResourceMap.set(pr.id, pr)

        temp.planetVoMap.get(pr.planetId).itemMap.set(pr.itemId, temp.itemMap.get(pr.itemId))
    }
    for (let pm of temp.planetMachineData) {
        temp.planetMachineMap.set(pm.id, pm)

        temp.planetVoMap.get(pm.planetId).machineMap.set(pm.itemId, temp.itemMap.get(pm.itemId))
    }

    // 重新计算电量
    for (let s of temp.starVoMap.values()) {
        for (let p of s.planetArray) {
            let energy = 0
            for (let m of p.machineMap.values()) {
                energy += m.energy
            }

        }
    }
}
export let data = reactive<Data>(temp)

let loop = () => {
    if (data.moveLeftTime > 0) {
        data.moveLeftTime--
        if (data.moveLeftTime == 0) {
            data.current.star = data.target.star
            data.current.planet = data.target.planet
        }
    }
    // for (let star of globalConfig.map) {
    //     for (let p of star.spl) {
    //         p.pe = 0
    //         for (let em of p.pem) {
    //             p.pe += em.now * em.power
    //         }
    //         p.pet = p.pe
    //     }
    // }
    // formulaLoop()

    if (data.lastSaveTime + 60000 < new Date().getTime()) {
        save()
    }
}

let save = () => {
    data.lastSaveTime = new Date().getTime()
    localStorage.setItem('save', JSON.stringify(data))
}

window.setInterval(loop, 1000)

// export let pause = () => {
//     cycle == null ? window.setInterval(loop, 1000) : clearInterval(cycle)
// }
